from DataLayer import ConnDBAndCloseDB
from DataLayer import UserDataLoad
from DataLayer import CargoDataLoad
from DataLayer import OrderFormDataLoad

import datetime

## 客服员信息加载
# 加载内容：
# 1.账号
# 2.头像
# 3.客服员昵称
# 4.总营业额
# 5.当月营业额
# 6.处理订单数
def waiterInfoLoad(account):
    conn, cur = ConnDBAndCloseDB.connectDB()
    waiter = UserDataLoad.selWaiterByAccount(cur, account)
    # print(waiter.toString())
    # 获取当月营销额
    # 获取当前年数月数
    today=datetime.datetime.today()
    year=today.year
    month=today.month
    FormSet=OrderFormDataLoad.selOrderFormByDateYearMonthAndWai_no(cur,year,month,waiter.no)
    monthTurnover=0
    for i in FormSet:
        monthTurnover+=i[4]
    # 获取处理订单总数
    allOrderForm=OrderFormDataLoad.selOrderFormByWai_no(cur,waiter.no)
    count=len(allOrderForm)
    dictTemp={
        'account':account,
        'photo':waiter.photo,
        'name':waiter.name,
        'turnover':waiter.turnover,
        'monthTurnover':monthTurnover,
        'orfoNum':count
    }
    ConnDBAndCloseDB.closeDB(cur, conn)
    return dictTemp

## 货物信息加载，客服员
# 加载内容：
# 1.货物号
# 2.图片
# 3.描述信息（）
# 4.价格
# 5.生产地址
# 6.实际存货量
# 7.最小存货量
# 8.删除标识
class CargoInfo:
    def __init__(self,carg_no,carg_desc,carg_photo,carg_price,carg_manu,carg_stock,carg_minStock,carg_delstatus):
        self.no=carg_no                         # 1.货物编号
        self.photo=carg_photo                   # 2.图片
        self.desc=carg_desc                     # 3.货物描述信息
        self.price=carg_price                   # 4.价格
        self.manu=carg_manu                     # 5.生产地址
        self.stock=carg_stock                   # 6.实际存货量
        self.minStock=carg_minStock             # 7.最小存货量
        self.delStatus=carg_delstatus           # 8.删除标识

    def toString(self):
        return "{} {} {} {} {} {} {} {} {}".format(self.no,self.photo,self.name,self.desc,self.manu,self.price,self.stock,self.minStock,self.delStatus)

# mode
# 1 所有
# 2 未删除
# 3 已删除
def cargoInfoLoadForWai(mode):
    conn, cur = ConnDBAndCloseDB.connectDB()
    allCargos=CargoDataLoad.selAllCargo(cur)
    cargoDict={}
    if mode==1:
        # 所有货物
        count=0
        for i in allCargos:
            elem=CargoInfo(i[0],i[-2],i[-3],i[1],i[2],i[3],i[4],i[-1])
            if elem.delStatus==0:
                delStatusStr='未删除'
            else:
                # elem.delStatus==1
                delStatusStr = '已删除'
            cargoDict[count]={'no':elem.no,
                              'photo':elem.photo,
                              'desc':elem.desc,
                              'manu':elem.manu,
                              'price':elem.price,
                              'stock':elem.stock,
                              'minStock':elem.minStock,
                              'delStatus':delStatusStr
                              }
            count+=1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return cargoDict
    elif mode==2:
        # 未删除货物
        count = 0
        for i in allCargos:
            if i[-1]==0:
                elem = CargoInfo(i[0], i[-2], i[-3], i[1], i[2], i[3], i[4], i[-1])
                if elem.delStatus == 0:
                    delStatusStr = '未删除'
                else:
                    # elem.delStatus==1
                    delStatusStr = '已删除'
                cargoDict[count] = {'no': elem.no, 'photo': elem.photo, 'desc': elem.desc, 'manu': elem.manu,
                                    'price': elem.price, 'stock': elem.stock, 'minStock': elem.minStock,
                                    'delStatus': delStatusStr}
                count += 1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return cargoDict
    else:
        # mode == 3
        # 已删除货物
        count = 0
        for i in allCargos:
            if i[-1] == 1:
                elem = CargoInfo(i[0], i[-2], i[-3], i[1], i[2], i[3], i[4], i[-1])
                if elem.delStatus == 0:
                    delStatusStr = '未删除'
                else:
                    # elem.delStatus==1
                    delStatusStr = '已删除'
                cargoDict[count] = {'no': elem.no, 'photo': elem.photo, 'desc': elem.desc, 'manu': elem.manu,
                                    'price': elem.price, 'stock': elem.stock, 'minStock': elem.minStock,
                                    'delStatus': delStatusStr}
                count += 1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return cargoDict

class OrderFormInfo:
    def __init__(self,no,date,desc,total,status):
        self.no=no
        self.date=date
        self.desc=desc
        self.total=total
        self.status=status

    def toString(self):
        return "{} {} {} {} {}".format(self.no,self.date,self.desc,self.total,self.status)
##订单信息加载，客服员
# 订单信息加载，已结算0
# 订单信息加载，申请退费1
# 订单信息加载，已完成2
# 订单信息加载，已退费3
# 订单信息加载，自己处理的全部订单4
# 返回信息
# 订单号，时间，账单描述，总金额，状态
def orderFormLoadForWai(account,mode):
    conn, cur = ConnDBAndCloseDB.connectDB()
    if mode==0:
        FormSet=OrderFormDataLoad.selAllOrderForm(cur)
        FormDict = {}
        count = 0
        for i in FormSet:
            if i[-3]==1:
                # 生成desc字符串
                desc = ""
                details = OrderFormDataLoad.selOrderDetialByorfo_no(cur, i[0])
                for j in details:
                    cargoName = CargoDataLoad.selCargoByCarg_no(cur, j[1]).desc.split(";")[0]
                    descElem=cargoName+"*"+str(j[2])
                    desc += descElem + "；"
                elem = OrderFormInfo(i[0], i[2], desc, i[3], i[-3])
                if elem.status == 0:
                    statusStr = '未结算'
                elif elem.status == 1:
                    statusStr = '已结算'
                elif elem.status == 2:
                    statusStr = '已完成'
                elif elem.status == 3:
                    statusStr = '已申请退费'
                else:
                    # elem.status==4
                    statusStr = '已退费'
                FormDict[count] = {'no': elem.no, 'date': str(elem.date), 'desc': elem.desc, 'total': str(elem.total),
                                   'status': statusStr}
                count += 1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return FormDict
    elif mode==1:
        FormSet = OrderFormDataLoad.selAllOrderForm(cur)
        FormDict = {}
        count = 0
        for i in FormSet:
            if i[-3] == 3:
                # 生成desc字符串
                desc = ""
                details = OrderFormDataLoad.selOrderDetialByorfo_no(cur, i[0])
                for j in details:
                    cargoName = CargoDataLoad.selCargoByCarg_no(cur, j[1]).desc.split(";")[0]
                    descElem=cargoName+"*"+str(j[2])
                    desc += descElem + "；"
                elem = OrderFormInfo(i[0], i[2], desc, i[3], i[-3])
                if elem.status == 0:
                    statusStr = '未结算'
                elif elem.status == 1:
                    statusStr = '已结算'
                elif elem.status == 2:
                    statusStr = '已完成'
                elif elem.status == 3:
                    statusStr = '已申请退费'
                else:
                    # elem.status==4
                    statusStr = '已退费'
                FormDict[count] = {'no': elem.no, 'date': str(elem.date), 'desc': elem.desc, 'total': str(elem.total),
                                   'status': statusStr}
                count += 1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return FormDict
    elif mode==2:
        waiter = UserDataLoad.selWaiterByAccount(cur, account)
        FormSet = OrderFormDataLoad.selOrderFormByWai_no(cur, waiter.no)
        FormDict = {}
        count = 0
        for i in FormSet:
            if i[-3] == 2:
                # 生成desc字符串
                desc = ""
                details = OrderFormDataLoad.selOrderDetialByorfo_no(cur, i[0])
                for j in details:
                    cargoName = CargoDataLoad.selCargoByCarg_no(cur, j[1]).desc.split(";")[0]
                    descElem=cargoName+"*"+str(j[2])
                    desc += descElem + "；"
                elem = OrderFormInfo(i[0], i[2], desc, i[3], i[-3])
                if elem.status == 0:
                    statusStr = '未结算'
                elif elem.status == 1:
                    statusStr = '已结算'
                elif elem.status == 2:
                    statusStr = '已完成'
                elif elem.status == 3:
                    statusStr = '已申请退费'
                else:
                    # elem.status==4
                    statusStr = '已退费'
                FormDict[count] = {'no': elem.no, 'date': str(elem.date), 'desc': elem.desc, 'total': str(elem.total),
                                   'status': statusStr}
                count += 1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return FormDict
    elif mode==3:
        waiter = UserDataLoad.selWaiterByAccount(cur, account)
        FormSet = OrderFormDataLoad.selOrderFormByWai_no(cur, waiter.no)
        FormDict = {}
        count = 0
        for i in FormSet:
            if i[-3] == 4:
                # 生成desc字符串
                desc = ""
                details = OrderFormDataLoad.selOrderDetialByorfo_no(cur, i[0])
                for j in details:
                    cargoName = CargoDataLoad.selCargoByCarg_no(cur, j[1]).desc.split(";")[0]
                    descElem=cargoName+"*"+str(j[2])
                    desc += descElem + "；"
                elem = OrderFormInfo(i[0], i[2], desc, i[3], i[-3])
                if elem.status == 0:
                    statusStr = '未结算'
                elif elem.status == 1:
                    statusStr = '已结算'
                elif elem.status == 2:
                    statusStr = '已完成'
                elif elem.status == 3:
                    statusStr = '已申请退费'
                else:
                    # elem.status==4
                    statusStr = '已退费'
                FormDict[count] = {'no': elem.no, 'date': str(elem.date), 'desc': elem.desc, 'total': str(elem.total),
                                   'status': statusStr}
                count += 1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return FormDict
    else:
        # mode == 4
        waiter = UserDataLoad.selWaiterByAccount(cur, account)
        FormSet = OrderFormDataLoad.selOrderFormByWai_no(cur, waiter.no)
        FormDict = {}
        count = 0
        for i in FormSet:
            # 生成desc字符串
            desc = ""
            details = OrderFormDataLoad.selOrderDetialByorfo_no(cur, i[0])
            for j in details:
                cargoName = CargoDataLoad.selCargoByCarg_no(cur, j[1]).desc.split(";")[0]
                descElem=cargoName+"*"+str(j[2])
                desc += descElem + "；"
            elem = OrderFormInfo(i[0], i[2], desc, i[3], i[-3])
            if elem.status == 0:
                statusStr = '未结算'
            elif elem.status == 1:
                statusStr = '已结算'
            elif elem.status == 2:
                statusStr = '已完成'
            elif elem.status == 3:
                statusStr = '已申请退费'
            else:
                # elem.status==4
                statusStr = '已退费'
            FormDict[count] = {'no': elem.no, 'date': str(elem.date), 'desc': elem.desc, 'total': str(elem.total),
                               'status': statusStr}
            count += 1
        ConnDBAndCloseDB.closeDB(cur, conn)
        return FormDict

# 前往发货
def deliverCargos(account,orfo_no):
    conn, cur = ConnDBAndCloseDB.connectDB()
    waiter=UserDataLoad.selWaiterByAccount(cur,account)
    orderForm=OrderFormDataLoad.selOrderFormByOrfo_no(cur,orfo_no)
    # 更改货物数量，若货物不足返回0，货物充足进行下一步
    # 根据订货单号，查找订货细则，订货细则的所有货物都应该充足，否则不能发货
    details=OrderFormDataLoad.selOrderDetialByorfo_no(cur,orfo_no)
    flag=1
    for i in details:
        need=i[2]
        supply=CargoDataLoad.selCargoByCarg_no(cur,i[1]).stocknum
        if int(need)>int(supply):
            flag=0
    if flag==0:
        ConnDBAndCloseDB.closeDB(cur, conn)
        return 0    # 货物不足
    else:
        # flag==1
        # 货物供给
        for i in details:
            need = i[2]
            cargo=CargoDataLoad.selCargoByCarg_no(cur,i[1])
            CargoDataLoad.modiCargoStock(conn,cur,cargo,waiter.no,cargo.stocknum-need)
        # 把已结算改成已完成
        OrderFormDataLoad.modiOrderFormStatus(conn,cur,orderForm,2)
        # 把订单的客服员编号改成自己的编号
        OrderFormDataLoad.modiOrderFormWai_no(conn,cur,orderForm,waiter.no)
        ConnDBAndCloseDB.closeDB(cur, conn)
        return 1

# 个人信息修改
def modiWaiterBasicInfo(account,accountAlter,name ):
    conn, cur = ConnDBAndCloseDB.connectDB()
    user = UserDataLoad.selUserBasicByAccount(cur, account)
    waiter = UserDataLoad.selWaiterByAccount(cur, account)
    userAnother = UserDataLoad.selUserBasicByAccount(cur, accountAlter)
    if userAnother:
        # 不修改账户名
        if userAnother.account==account:
            UserDataLoad.modiWaiterName(conn, cur, waiter, name)
            ConnDBAndCloseDB.closeDB(cur, conn)
            return 1        # 不修改账户名，只修改地址
        # 账户名与其他账户名重名
        else:
            ConnDBAndCloseDB.closeDB(cur, conn)
            return 0        # 重名
    else:
        # 修改账户名和地址
        UserDataLoad.modiWaiterName(conn, cur, waiter, name)
        UserDataLoad.modiUserBasicAccount(conn, cur, user, accountAlter)
        ConnDBAndCloseDB.closeDB(cur, conn)
        return 2

# 修改密码
def modiWaiterPwd(account, oldPwd, newPwd, affirmPwd):
    conn, cur = ConnDBAndCloseDB.connectDB()
    waiter = UserDataLoad.selWaiterByAccAndPwd(cur, account, oldPwd)
    if not waiter:
        ConnDBAndCloseDB.closeDB(cur, conn)
        return -1  # 原密码输入错误

    else:
        if newPwd == affirmPwd:
            # 数据库修改密码
            user = UserDataLoad.selUserBasicByAccount(cur, account)
            UserDataLoad.modiUserBasicPwd(conn, cur, user, newPwd)
            ConnDBAndCloseDB.closeDB(cur, conn)
            return 1  # 正确
        else:
            ConnDBAndCloseDB.closeDB(cur, conn)
            return 0  # 新密码与确认密码不相符

# 删除货物
def deleteCargo(account, carg_no):
    conn, cur = ConnDBAndCloseDB.connectDB()
    cargo=CargoDataLoad.selCargoByCarg_no(cur,carg_no)
    waiter=UserDataLoad.selWaiterByAccount(cur,account)
    CargoDataLoad.modiCargoDelStatus(conn,cur,cargo,waiter.no,1)
    return 1

if __name__ == '__main__':
    waiterInfoLoad("1796915552")